// pages/staff/my/chooseteacher.js
const app = getApp()
var CONFIG = require("../../../config/config.js");
var share = require("../../../utils/share.js");
Page({

    /**
     * 页面的初始数据
     */
    data: { 
        area_team: [],  //校区列表
        area_index: 0,  //校区下标
        area_id: '',  //校区下标
        teacher_cats: [{ 'name': '请选择', 'id': '0'}],
        teacher_index: 0, 
        consultant_id:'',
        showfalse: false,
        showfalse1: false,
        text:'',
        stuPhone:'',  //学员手机号
        tel:'',   //原课程顾问手机号
        identity:2,
        learn_index: 0, 
        learn_cats: [{ 'name': '请选择', 'id': '0' }],
    },

    // 校区选择
    areaeChange(e) {
        var that = this;
        var index = e.detail.value;
        that.setData({
            area_index: index,
        })
        if(Number(index)==0){
            that.setData({
                area_id: ''
            })
        }else{
            that.setData({
                area_id: that.data.area_team[index].id
            })
            that.getCourse();
        }
    },

    // 获取校区列表
    teamlist() {
        wx.showLoading({
            title: '加载中~',
        })
        var that = this;
        share.wxRequest('crmapi/User/team', {}, function (res) {
            wx.hideLoading()
            if (Number(res.data.errcode) == 0) {
                var data = res.data.data;
                var obj = [{
                    id: 0,
                    title: '请选择校区'
                }]
                var data1 = obj.concat(data);
                that.setData({
                    area_team: data1
                })
                if (Number(data1.length) == 2) {
                    that.setData({
                        area_index: 1,
                        area_id: data1[1].id,
                    })
                    that.getCourse();
                }
            }
        })
    },

    learnlist(){
        wx.showLoading({
            title: '加载中~',
        })
        var that = this;
        share.wxRequest('crmapi/Learn/learnTeacher', {}, function (res) {
            wx.hideLoading()
            if (Number(res.data.errcode) == 0) {
                var data = res.data.data;
                var obj = [{
                    id: 0,
                    name: '请选择学管师'
                }]
                var data1 = obj.concat(data);
                that.setData({
                    learn_cats: data1
                })
            }
        })
    },
    
    


    // 选择课程顾问
    bindteachCatChange(e){
        var that = this;
        var index = e.detail.value;
        if (Number(index) == 0) {
            wx.showToast({
                title: '请先选择课程顾问',
                icon: 'none',
                duration: 3500
            })
        } else {
            var teacher_catstitle = that.data.teacher_cats;
            var id = teacher_catstitle[index].id;
            that.setData({
                teacher_index: index,
            })
        }
    },


    // 获取课程顾问
    getCourse() {
        var that = this;
        wx.showLoading({
            title: '加载中~',
        })
        share.wxRequest('crmapi/User/consList', { team_id: that.data.area_id}, function (res) {
            wx.hideLoading();
            if (Number(res.data.errcode) == 0) {
                var data = res.data.data.lists;
                var teacher_cats = that.data.teacher_cats.concat(data);
                that.setData({
                    teacher_cats: teacher_cats
                })

            }

        })
    },

    bindlearnCatChange(e){
        var that = this;
        var index = e.detail.value;
        if (Number(index) == 0) {
            wx.showToast({
                title: '请先选择学管师',
                icon: 'none',
                duration: 3500
            })
        } else {
            var learn_catstitle = that.data.learn_cats;
            var id = learn_catstitle[index].id;
            that.setData({
                learn_index: index,
            })
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        that.setData({
            stuPhone: options.stuPhone,
            identity: app.globalData.identity,
        })
        if (Number(app.globalData.identity) == 3) {
            that.learnlist();
        }else{
            that.teamlist();
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var that = this;
        var tel = wx.getStorageSync('tel');
        that.setData({
            tel: tel
        })
    },

    
    sureclick(){
        var that = this;
        that.setData({
            showfalse:false
        })
        wx.navigateBack(-1);
    },

    sureclick1(){
        var that = this;
        that.setData({
            showfalse1: false
        })
    },


    // 点击提交
    subbut() {
        var that = this;
        var add_catstitle = that.data.add_cats;
        var teacher_catstitle = that.data.teacher_cats;
        var learn_catstitle = that.data.learn_cats;
        var add_index = that.data.add_index;
        var teacher_index = that.data.teacher_index;
        var learn_index = that.data.learn_index;
        if (Number(teacher_index) == 0&&Number(that.data.identity)!=3) {
            wx.showToast({
                title: '请选择课程顾问',
                icon: 'none',
                duration: 3500
            })
        }else if (Number(learn_index) == 0 && Number(that.data.identity) == 3) {
            wx.showToast({
                title: '请选择学管师',
                icon: 'none',
                duration: 3500
            })
        } else {
            if (Number(that.data.identity) == 3){
                var phone = learn_catstitle[learn_index].phone;    //课程顾问手机号
                var url = 'crmapi/Learn/learnTransferStu'
            }else{
                var phone = teacher_catstitle[teacher_index].phone;    //课程顾问手机号
                var url = 'crmapi/user/consTransferStu'
            }
            wx.showLoading({
                title: '提交中~',
            })
            
            var data = {
                old_con_phone: that.data.tel,
                stuPhone: that.data.stuPhone,
                new_con_phone: phone,
            }
            share.wxRequest(url, data, function (res) {
                // console.log(res);
                wx.hideLoading();
                if (Number(res.data.errcode) == 0) {
                    that.setData({
                        text: res.data.msg,
                        showfalse: true,
                    })
                }else{
                    that.setData({
                        text: res.data.msg,
                        showfalse1: true,
                    })
                }
            })
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})